﻿using AnlianCommon.Common.Domains.BeginningManagement;
using AnlianCommon.Common.Domains.ProductionManagement;
using AnlianCommon.Common.Domains.SheepFarmHouse;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace SheepCommon.API.SheepFarmHomepage.RealTimeStorage
{
    public class RealTimeStorageQueryHandler : IRequestHandler<RealTimeStorageQueryCommand, Result>
    {
        private readonly IBaseRepository<SheepArchives> _sheepArchivesRepository;
        private readonly IBaseRepository<Startofseason> _startofseasonRepository;
        private readonly IBaseRepository<Cottage> _cottageRepository;
        private readonly IBaseRepository<Field> _fieldRepository;

        public RealTimeStorageQueryHandler(IBaseRepository<SheepArchives> sheepArchivesRepository, IBaseRepository<Startofseason> startofseasonRepository, IBaseRepository<Cottage> cottageRepository, IBaseRepository<Field> fieldRepository)
        {
            _sheepArchivesRepository = sheepArchivesRepository;
            _startofseasonRepository = startofseasonRepository;
            _cottageRepository = cottageRepository;
            _fieldRepository = fieldRepository;
        }
        /// <summary>
        /// 羊场实时存栏种公种母后裔
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(RealTimeStorageQueryCommand request, CancellationToken cancellationToken)
        {
            var reseult = new Result();
            try
            {
                var realTimeStorageQuery = await (from a in await _sheepArchivesRepository.GetAll()
                                                  where a.CottageId != null && a.FieldId != null
                                                  select new RealTimeStorageQueryDto()
                                                  {
                                                      RecordType = a.RecordType == 0 ? "种母" : a.RecordType == 1 ? "种公" : "后裔",
                                                  }).ToListAsync();
                realTimeStorageQuery = await (from a in await _sheepArchivesRepository.GetAll()
                                              where a.CottageId != null && a.FieldId != null
                                              group a by a.RecordType into a
                                              select new RealTimeStorageQueryDto()
                                              {
                                                  RecordType  = a.Key == 0 ? "种母" : a.Key == 1 ? "种公" : "后裔",
                                                  value = a.Count(),

                                              }).ToListAsync();
                reseult.Data = realTimeStorageQuery;

            }
            catch (Exception)
            {

                throw;
            }
            return reseult;
        }
    }
}
